package org.mspring.mlog.web.module.admin.bbs;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.bbs.Topic;
import org.mspring.mlog.service.bbs.TopicReplyService;
import org.mspring.mlog.service.bbs.TopicService;
import org.mspring.mlog.web.module.admin.Admin_AbstractWidget;
import org.mspring.mlog.web.query.bbs.TopicQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 帖子管理
 * 
 * @author Gao Youbo
 * @since 2013年9月5日
 */
@RequestMapping("/admin/bbs/topic")
@Widget
public class Admin_TopicWidget extends Admin_AbstractWidget {
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicReplyService topicReplyService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/list")
    public String list(@ModelAttribute Page<Topic> topicPage, @ModelAttribute Topic topic, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (topicPage == null) {
            topicPage = new Page<Topic>();
        }
        if (topicPage.getSort() == null) {
            topicPage.setSort(new Sort("id", Sort.DESC));
        }
        topicPage = topicService.listPage(new TopicQueryCriterion(queryParams), topicPage);
        model.addAttribute("topicPage", topicPage);
        model.addAllAttributes(queryParams);
        return "/admin/bbs/topic/listTopic";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(required = false) Long[] id, @ModelAttribute Page<Topic> topicPage, @ModelAttribute Topic topic, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null || id.length == 0) {
            return prompt(model, "请选择要删除的帖子");
        }
        topicService.deleteTopic(id);
        return list(topicPage, topic, queryParams, request, response, model);
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, @ModelAttribute Topic topic, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return prompt(model, "请选择要修改的帖子");
        }
        topic = topicService.get(id);
        if (topic == null) {
            return prompt(model, "没找到要删除的帖子");
        }
        model.addAttribute("topic", topic);
        return "/admin/bbs/topic/editTopic";
    }

    @RequestMapping("/edit/save")
    public String edit_save(@ModelAttribute Topic topic, HttpServletRequest request, HttpServletResponse response, Model model) {
        topicService.updateTopic(topic);
        return prompt(model, "修改成功", "/admin/bbs/topic/edit?id=" + topic.getId());
    }
}
